﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Threading;
using FB.Bussiness;
using log4net;

namespace FB
{
    public class Global : System.Web.HttpApplication
    {
        private static readonly ILog log = LogManager.GetLogger("Global");
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            log4net.Config.XmlConfigurator.Configure(System.Configuration.ConfigurationManager.GetSection(
                "log4net") as System.Xml.XmlElement);
            log.Info("1 Do Application_Start");

            // Create thread to execute feed chanel task
            ThreadStart tsFeedChanel = new ThreadStart(DoFeedChanel);
            Thread FeedChanelTask = new Thread(tsFeedChanel);
            FeedChanelTask.Start();

            // Create thread to execute check video task
            ThreadStart tsCheckVideo = new ThreadStart(DoCheckVideo);
            Thread CheckVideoTask = new Thread(tsCheckVideo);
            CheckVideoTask.Start();
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
            log.Info("1 Application End");
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            log.Info("1 Application Error");
        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }


       static void DoFeedChanel()
       {
           // In this example, task will repeat in infinite loop
           // You can additional parameter if you want to have an option
           // to stop the task from some page
           while (true)
           {
               log.Info("2 Loop to do the feed task");
               // Execute scheduled task
               FeedTask();
               // Wait for certain time interval
               System.Threading.Thread.Sleep(TimeSpan.FromMinutes(30));
           }
       }
   
       static void FeedTask()
       {
           log.Info("3 feed task");
           ChanelBO BO = new ChanelBO();
           BO.DoFeedChanelJob();
       }


       static void DoCheckVideo()
       {
           // In this example, task will repeat in infinite loop
           // You can additional parameter if you want to have an option
           // to stop the task from some page
           while (true)
           {
               log.Info("II Loop to do the check video task");
               // Execute scheduled task
               CheckVideo();
               // Wait for certain time interval
               System.Threading.Thread.Sleep(TimeSpan.FromMinutes(13));
           }
       }

       static void CheckVideo()
       {
           log.Info("III CheckVideo");
           VideoBO BO = new VideoBO();
           BO.DoCheckVideoJob();
       }

    }
}
